#include "stdafx.h" 
#include <windows.h>
#include <shellapi.h>
#include <msclr\marshal.h>
#include <strsafe.h>

using namespace System;
using namespace System::IO;
using namespace msclr::interop;

#include "FileOperationCmds.h"
#include "ConceptCmd.h"


String^ cConceptCmd::GetDestFolderPath(String^ dirPath)
{
    WIN32_FIND_DATA ffd;
    HANDLE hFind = INVALID_HANDLE_VALUE;
    TCHAR szDir[MAX_PATH];
    marshal_context^ context = gcnew marshal_context();

    StringCchCopy(szDir, MAX_PATH, context->marshal_as<const TCHAR*>(dirPath));
    StringCchCat(szDir, MAX_PATH, TEXT("\\*"));

    hFind = FindFirstFile(szDir, &ffd);
    if (INVALID_HANDLE_VALUE == hFind) 
    {
        return ("");
    } 

    String^ dirFileName = "";
    bool foundDirFile = false;
    do 
    {
        if (!(ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))
        {
            String^ fileName = gcnew String(ffd.cFileName, 0, MAX_PATH);
            fileName = fileName->Substring(0, fileName->IndexOf(L''+L''));
            if (true == fileName->Contains("directory"))
            {
                dirFileName = dirPath + "\\" + fileName;
                foundDirFile = true;
            }
        }
    } while (FindNextFile(hFind, &ffd) != 0);
    FindClose(hFind);
    
    if ( true == foundDirFile)
    {
        RichTextBox^ dirFile = gcnew RichTextBox;
        dirFile->Visible = false;
        dirFile->LoadFile( dirFileName, RichTextBoxStreamType::PlainText );
        String^ lineStr = "";
        for (int i=0; i<dirFile->Lines->Length; i++)
        {
            lineStr = dirFile->Lines[i];
            if (0 < lineStr->Length && true == lineStr->Contains(":\\"))
            {
                return (lineStr);
            }
        }

        return ("");
    } 
    else
    {
        return ("");
    }
}

/*
void cConceptCmd::ModifyConceptLib(String^ libDirPath, String^ inputFileName)
{
    String^ ComponentFolder = FindFolder(libDirPath, "ASIC");
    if ( "" == ComponentFolder )
    {
        //MessageBox.Show("Khong tim thay folder nao co ten ASIC");
        return;
    }

    RichTextBox^ inputFile = gcnew RichTextBox;
    inputFile->Visible = false;
    inputFile->LoadFile( inputFileName, RichTextBoxStreamType::PlainText );
    String^ oldPart = "";
    String^ oldPartName = "";
    String^ oldPartNumber = "";
    String^ newPart = "";
    String^ newPartName = "";
    String^ newPartNumber = "";

    int idx = inputFile->Lines[0]->IndexOf(",");
    oldPart         = inputFile->Lines[0]->Substring(0, idx);
    oldPart         = oldPart->ToUpper();
    oldPartName     = oldPart->Substring(0, oldPart->IndexOf('-'));
    int i = oldPart->IndexOf('-') + 1;
    oldPartNumber   = oldPart->Substring(i, oldPart->Length - i);

    newPart = inputFile->Lines[0]->Substring(idx + 1, inputFile->Lines[0]->Length - (idx + 1));
    newPart = newPart->ToUpper();
    newPartName = newPart->Substring(0, newPart->IndexOf('-'));
    i = newPart->IndexOf('-') + 1;
    newPartNumber   = newPart->Substring(i, newPart->Length - i);

    String^ oldPartFolder = FindFolder(ComponentFolder, oldPartName);
    if ( "" == oldPartFolder )
    {
        //MessageBox.Show("Khong tim thay folder nao co ten ASIC");
        return;
    }
    oldPartFolder = oldPartFolder->ToUpper();

    String^ newPartFolder = oldPartFolder->Replace(oldPartName, newPartName)->Replace(oldPartNumber, newPartNumber);

    if ( 0 != RenameFolder(oldPartFolder, newPartFolder))
    {
        return;
    }

    String^ chipsFolder = FindFolder(newPartFolder, "chips");
    if ( "" == chipsFolder )
    {
        //MessageBox.Show("Khong tim thay folder nao co ten ASIC");
        return;
    }
    chipsFolder = chipsFolder->ToUpper();

    String^ chipsFileName = FindFile(chipsFolder, "chips");
    if ("" == chipsFileName)
    {
        return;
    }

    RichTextBox^ chipsFile = gcnew RichTextBox;
    chipsFile->Visible = false;
    chipsFile->LoadFile( chipsFileName, RichTextBoxStreamType::PlainText );
    
    String^ strLines = "";
    array<String^>^ lines = chipsFile->Lines;

    for (int l=0; l<lines->Length; l++)
    {
        strLines = lines[l]->ToUpper();
        if (true == strLines->Contains("PRIMITIVE"))
        {
            strLines = strLines->Replace(oldPart, newPart);
            lines[l] = strLines;
        } 
        else if (true == strLines->Contains("PART_NAME"))
        {
            strLines = strLines->Replace(oldPart, newPart);
            lines[l] = strLines;
        }
        else if (true == strLines->Contains("BODY_NAME"))
        {
            strLines = strLines->Replace(oldPart, newPart);
            lines[l] = strLines;
        }
    }

    chipsFile->Lines = lines;
    chipsFile->SaveFile(chipsFileName, RichTextBoxStreamType::PlainText);
}
*/
